
import { UserPermission, Space, Equipment } from '@/api/home'
const state = {
    UserPermissionList: [],
    SpaceList: [],
    EquipmentList: [],
    homeDataList: [],
}

const mutations = {
    SET_UserPermission_DATA: (state, dataList) => {
        state.UserPermissionList = dataList;
    },
    SET_Space_DATA: (state, dataList) => {
        state.SpaceList = dataList;
    },
    SET_Equipment_DATA: (state, dataList) => {
        state.EquipmentList = dataList;
        state.homeDataList = dataList;
    },
    SET_HOME_DATA: (state, dataList) => {
        let homeDataList = [];
        state.UserPermissionList.map(item => {
            for( let k = 0; k < item.pdList.length; k++) {
                let obj = null;
                for (let i = 0; i < state.SpaceList.length; i++) {
                    if (item.pdList[k].resourceID == state.SpaceList[i].spaceID) {
                        obj = {Space: state.SpaceList[i]};
                    }
                }
                for (let i = 0; i < state.EquipmentList.length; i++) {
                    if (item.pdList[k].resourceID == state.EquipmentList[i].equipmentID) {
                        obj = {Equipment: state.EquipmentList[i]};
                    }
                }
                if (obj) {
                    homeDataList.push(obj)
                }
            }
        });
        state.homeDataList = homeDataList;
    },
}

const actions = {
    UserPermission: ({ commit }, params) => {
      return new Promise((resolve, reject) => {
        UserPermission({ userid: params.userid }).then(response => {
          const { data } = response
          commit('SET_UserPermission_DATA', data)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },
    Space: ({ commit }, params) => {
        return new Promise((resolve, reject) => {
            Space({ resourceID: ''}).then(response => {
            const { data } = response
            commit('SET_Space_DATA', data)
            resolve(data)
          }).catch(error => {
            reject(error)
          })
        })
      },
      Equipment: ({ commit }, params) => {
        return new Promise((resolve, reject) => {
          Equipment(params).then(response => {
            const { data } = response
            commit('SET_Equipment_DATA', data?data: [])
            resolve(data)
          }).catch(error => {
            reject(error)
          })
        })
      }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
